//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp.utility;
import java.io.FileOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	
	private Context mContext;
	private static Context sContext;
	private static String apiKey;
	
	public DatabaseHelper(Context applicationContext, String databaseName) {
		super(applicationContext, databaseName, null, 1);
		if (sContext == null) sContext = applicationContext;
		mContext = applicationContext;
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		initialize();
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	
	
	public boolean isInitialized(){
		try {
			FileInputStream f = new FileInputStream("/data/data/org.prx.prp/databases/org.prx");
			f.close();
			return true;
		} catch(Exception e) {
			return false;
		}
	}
	
	public void initialize() {
		if (isInitialized()) return;
		
		File f = new File("/data/data/org.prx.prp/databases/org.prx");
		if (!f.exists()) {
			try {
				File f1 = new File("/data/data/org.prx.prp/databases");
				f1.mkdir();
				f.createNewFile();
			} catch (IOException e) {
				Log.d("PRPAND", "There was a problem with the filesystem. ");
				Log.d("PRPAND", e.getMessage());
			}
		}
		
		try {
			InputStream in = mContext.getAssets().open("database.mp3");
			String outFilename = "/data/data/org.prx.prp/databases/org.prx";
			FileOutputStream out = new FileOutputStream(outFilename);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			out.close();
			in.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	}
	
	public static String getApiKey(){
		
		if (apiKey != null) return apiKey;
		
		if (sContext == null) {
			return "null";
		}
		try {
            InputStream in = sContext.getAssets().open("api_key");
            byte[] buf = new byte[32];
            in.read(buf);
            in.close();
  
            apiKey = new String(buf);
    
    
        } catch (IOException e) {
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "You need to put an api"
                    + " key into the assets directory"
                    + " in a file called api_key.");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
            Log.d("PRPAND", "**********************");
        }
        return apiKey;
	}
}

